//
//  WBHomeViewController.swift
//  mySwiftVV
//
//  Created by terrylee on 2016/12/10.
//  Copyright © 2016年 terry. All rights reserved.
//

import UIKit

//定义全局常量, 尽量使用private修饰, 否则到处都可以访问
private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
    }
    
    /// 列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    
    override func loadData() {
        
        // 用网络工具加载 微博 数据
        
        print("准备刷新,最后一条 \(self.listViewModel.statusList.last?.text)")
        listViewModel.loadStatus(pullup: self.isPullup) { (isSucess,shouldRefresh) in
            
            print("加载数据结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            
            //恢复上拉刷新标记
            self.isPullup = false
            
            //刷新tableview
            if shouldRefresh{
                
                self.tableView?.reloadData()
            }
        }
        
    }
    
    /// 显示好友
    @objc fileprivate func showFriends(){
        print(#function)
        
        let vc = WBDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }


}

// MARK: - 表格数据源方法 , 具体的数据源方法,不需要super
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2.设置cell
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        
        //3.返回cell
        return cell
    }
    
}

// MARK: - 设置界面
extension WBHomeViewController{
    //
    override func setupTableView() {
        
        super.setupTableView()
        
        //设置导航栏按钮
//        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        
        //swift 调用OC 返回 instancetype 的方法, 判断不出来是否可选
//        let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//        
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
//        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: <#T##CGFloat#>, target: <#T##AnyObject?#>, action: <#T##Selector#>)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        
        tableView?.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellId)
        
        setupNavTitle()
        
    }
    
    /// 设置导航栏标题
    private func setupNavTitle(){
        
        let title = WBNetWorkManager.shared.userAccount.screen_name
        
        
        let button = WBTitleButton(title: title)
        
        
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        
        
    }
    
    @objc func clickTitleButton(btn:UIButton){
        
        //设置选中状态
        btn.isSelected = !btn.isSelected
        
    }
    
}
